//
//  APLibs_APTextView_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2024/1/4.
//

import UIKit
import APExtensions
import Then

class APLibs_APTextView_DemoVC: UIViewController {
    private var textView: KLTextView!
    private var saveButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        
        initTextViewUI()
        initSaveButtonUI()
    }
    
    private func initTextViewUI() {
        textView = KLTextView(frame: CGRect(x: 15, y: 150, width: ap_kScreenW - 60, height: 58), style: .maxMinHeight).then({ make in
            view.addSubview(make)
            make.backgroundColor = .ap.hex("F7F7F7")
            make.layer.masksToBounds = true
            make.layer.cornerRadius = 9
            
            make.maximumHeight = 140
            make.minimumHeight = 58
            make.textContainerInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 1)
            
            make.placeholder = "请输入圈子描述"
            make.font = UIFont.systemFont(ofSize: 13)
            make.textColor = .ap.hex("333333")
            
            make.configureMaxTextCount(500, font: UIFont.systemFont(ofSize: 13), textColor: .ap.hex("999999"), isSingleLine: false)
        })
        
        #if DEBUG
        textView.text = "Learn how you can highlight your app's Now Playing information on every platform. We'll take you through an overview of media metadata, learn how it gets represented in areas like the Lock Screen and Control Center, and show you how to write and publish effective media metadata for your content. We'll also explore how your app can respond to commands from other devices such as HomePod."
        #endif
        
        textView.didChangedTextBlcok = { tv in
            print("KLTextView textDicChanged \(tv.text ?? "")")
        }
        
        textView.didChangedHeightBlcok = { (tv, height) in
            print("KLTextView textDicChangedHeight \(tv.text ?? ""), height is \(height)")
        }
    }
    
    private func initSaveButtonUI() {
        saveButton = UIButton(type: .custom).then({ make in
            view.addSubview(make)
            make.frame = CGRect(x: 50, y: 500, width: 300, height: 44)
            make.setTitle("保存", for: .normal)
            make.backgroundColor = .gray
//            make.setBackgroundImage(.ap.image(withColor: .ap.hex("FFF6B3")), for: .normal)
//            make.setBackgroundImage(.ap.image(withColor: .ap.hex("FFE100")), for: .selected)
            make.setTitleColor(.ap.hex("222222"), for: .normal)
            make.titleLabel?.font = .systemFont(ofSize: 17)
            make.layer.masksToBounds = true
            make.layer.cornerRadius = 22
            make.isUserInteractionEnabled = false
            make.addTarget(self, action: #selector(saveAction), for: .touchUpInside)
        })
    }

    @objc private func saveAction() {
        print("保存成功 - \(textView.text ?? "")")
    }
}
